package com.controller;

import com.mapper.AdminMapper;
import com.vo.adminVo.AdminInfoCompleteRequestVo;
import com.vo.adminVo.AdminLoginResponseVo;
import com.vo.adminVo.AdminUpdatePasswordRequestVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * @Describe 管理员信息控制器
 */
@Controller
public class AdminController {

    @Resource
    AdminMapper adminMapper;


    /**
     * 修改管理员密码
     * @param vo
     * @param request
     * @return
     */
    @RequestMapping("/modify")
    @ResponseBody
    public String modify(AdminUpdatePasswordRequestVo vo, HttpServletRequest request) {
        Object session=request.getSession().getAttribute("user");
        List<AdminLoginResponseVo> adminLoginResponseVoList = adminMapper.getAdminLoginInfo();
        for(int i=0;i<adminLoginResponseVoList.size();i++)
        {
            AdminLoginResponseVo admin=adminLoginResponseVoList.get(i);
            if(admin.getPhone().equals(vo.getPhone())) {
                if (admin.getPassword().equals(vo.getOldPassword())) {
                    if(admin.getPassword().equals(vo.getNewPassword()) || admin.getPassword().equals(vo.getCheckPassword())) {
                        return "新密码不能与旧密码重复！";
                    }
                    else if (vo.getNewPassword() != "" && vo.getCheckPassword() != "") {
                        if (vo.getNewPassword().equals(vo.getCheckPassword())) {
                            if (admin.getLimit() == 1 && !admin.getPhone().equals(session)) {
                                return "不允许修改其他管理员账号";
                            }
                            else {
                                vo.setUpdateTime(LocalDateTime.now());
                                adminMapper.updateAdminPassword(vo);
                                return "修改成功";
                            }
                        }
                        else if(!vo.getNewPassword().equals(vo.getCheckPassword())) {
                            return "验证新密码失败！";
                        }
                    }
                    else if (vo.getNewPassword() == "" || vo.getCheckPassword() == "") {
                        return "密码不能为空";
                    }
                }
                else if (!admin.getPassword().equals(vo.getOldPassword())) {
                    return "原密码错误无法更改";
                }
            }
            else if (!admin.getPhone().equals(vo.getPhone()) && adminLoginResponseVoList.size()==i+1){
                return "此账号不存在";
            }
        }
        return "ok";
    }


    /**
     * 修改管理员密码验证
     * @param phone
     * @param request
     * @return
     */
    @RequestMapping("/update_admin_verify")
    @ResponseBody
    public String updateAdminVerify(String phone,HttpServletRequest request) {
        Object session=request.getSession().getAttribute("user");
        List<AdminLoginResponseVo> adminLoginResponseVoList = adminMapper.getAdminLoginInfo();
        for(int i=0;i<adminLoginResponseVoList.size();i++)
        {
            AdminLoginResponseVo admin=adminLoginResponseVoList.get(i);
            if(admin.getPhone().equals(phone)) {
                if(admin.getLimit().equals(1) && !admin.getPhone().equals(session)) {
                    return "无法修改其他管理员账户密码";
                }
                else {
                    return "账号存在";
                }
            }
            else if(!admin.getPhone().equals(phone) && adminLoginResponseVoList.size()==i+1){
                return "此账号不存在";
            }
        }
        return "未进入修改管理员密码验证";
    }

    /**
     * 完善管理员个人信息(未开始）
     * @param vo
     * @param request
     * @return
     */
    @RequestMapping("/complete_AdminInfo")
    @ResponseBody
    public String completeAdminInfo(AdminInfoCompleteRequestVo vo, HttpServletRequest request){
        return null;
    }

    /**
     * 更改管理员头像（合并到完善管理员个人信息模块）
     * @param
     * @param
     * @return
     */
//    @RequestMapping("/replace")
//    @ResponseBody
//    public String replace(AdminInfoCompleteRequestVo vo,String src,HttpServletRequest request)
//    {
//        Object session=request.getSession().getAttribute("user");
//        String[] s=src.split("http://localhost:8080");
////        employeeMapper.updateImage(session.toString(),s[1]);
////        adminMapper.completeAdminInfo();
//        return "保存成功";
////        return null;
//    }

    @RequestMapping("/adminInfo_complete")
    @ResponseBody
    public String adminInfoComplete(AdminInfoCompleteRequestVo vo,MultipartFile file) throws IOException{
        String filename = UUID.randomUUID() + "_" + file.getOriginalFilename();
        File dirs = new File("src/main/resources/static/images/admin"+filename);
        if (file.isEmpty()){
            return "头像上传失败";
        }
        else{
            file.transferTo(new File(dirs.getAbsolutePath()));
            vo.setNewImage("/static/images/admin/" + filename);
            vo.setUpdateTime(LocalDateTime.now());
            adminMapper.completeAdminInfo(vo);
            return "添加成功";
        }
    }


    /**
     * 获取管理员头像显示在前端
     * @param name
     * @param request
     * @return
     */
    @RequestMapping("/getAdminImage")
    @ResponseBody
    public String setImage(String name,HttpServletRequest request) {
        Object session=request.getSession().getAttribute(name);
        String image=  adminMapper.getAdminImage(session.toString());
        return image;
    }

    /**
     * 获取管理员姓名显示在前端
     * @param name
     * @param request
     * @return
     */
    @RequestMapping("/getAdminName")
    @ResponseBody
    public String setName(String name,HttpServletRequest request) {
        Object session=request.getSession().getAttribute(name);
        String Name=  adminMapper.getAdminName(session.toString());
        return Name;
    }


    /**
     * 上传管理员头像(本地保存以及数据库存储名称转换）
     * @param file
     * @return
     */
    @RequestMapping("/up")
    public String upload (@RequestParam("file") MultipartFile file) {
        // 获取原始名字
        String fileName = file.getOriginalFilename();
        fileName = file.getOriginalFilename();
        // 获取后缀名
        // String suffixName = fileName.substring(fileName.lastIndexOf("."));
        // 文件保存路径
        String filePath = "g:/summer/src/main/resources/templates/images/";
//        G:\summer\src\main\resources\templates
        // 文件重命名，防止重复
        fileName =UUID.randomUUID() + fileName;
        // 文件对象
        File dest = new File(fileName);
        // 判断路径是否存在，如果不存在则创建
        if(!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            // 保存到服务器中
            file.transferTo(dest);
        return "manager";

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "manager";
    }

}
